package cn.itsource.web;

import cn.itsource.domain.User;
import cn.itsource.sentinelhandler.GlobalHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
@RequestMapping("/user")
public class UserController {

    @Value("${server.port}")
    private String  port;

    @GetMapping("/{id}")
    @SentinelResource(value = "getUser",
            blockHandlerClass = GlobalHandler.class,
            blockHandler = "UserblockHandler",
            fallbackClass = GlobalHandler.class,
            fallback = "UserFallBackHandler"
    )
    public User getUser(@PathVariable("id") Long id){
        Random random = new Random();
        int nextInt = random.nextInt(3);
        System.out.println(nextInt );
        if(nextInt == 0) {
            int i = 1 / 0;
        }
        return new User(id,"强强","用户信息："+port);

    }


}
